$(function () {

    var classesId = $('#id').val();
    //话题类型
    var classify =null;

    // 加载话题
    classesTopicList(1,null,null);

    //班级成员上下组切换
    $('#carousel-class-member').carousel({interval: false});
    lazyCourseImage();
    /**
     * 班级话题
     * @param pageNo
     * @param order 排序
     */
    function classesTopicList(pageNo,that,classify) {
        var order = $('#topic-list').prev().find('.active').data('order');
        var joined = $('#joined').val();
        $.ajax({
            url: baseloaction + '/web/classes/topic/ajaxTopicList',
            data: {'currentPage': pageNo, 'classesId': classesId, 'order': order, 'classify':classify,'joined':joined},
            dataType: 'html',
            beforeSend: function () {
                if (pageNo == 1) {
                    $('#topic-list section.no-data__wrap').prevAll('section').remove();
                }
                $('span.loading').parent().removeClass('hide');
                $('div.class-slide-more__wrap button').addClass('disabled');
            },
            success: function (html) {
                $(html).insertBefore($('#topic-list span.loading').parent());
                var pageEnd = $('#pageEnd').val();
                $('span.loading').parent().addClass('hide');
                $('div.class-slide-more__wrap').toggleClass('hide', pageNo >= pageEnd);
                $('section.no-data__wrap').toggleClass('hide', pageEnd != 0);
                if (pageNo < pageEnd) {
                    jQuery('div.class-slide-more__wrap button')
                        .data('page', pageNo + 1)
                        .data('order', order)
                        .removeClass('disabled');
                }
                $('#pageEnd').remove();
                $('#topic-list > hr').remove();
                //图像懒加载
                lazyImage();
            }
        })
    }

    // 加载班级成员
    classesMemberList();

    $('body')
        .on('click', '#classes-topic-publish', function () {
            var classesId = $('#id').val();
            window.location = baseloaction + '/web/classes/topic/initPublish?classesId=' + classesId;
        })
        .on('click', '#classes-join', function () {
            isLogin(function () {
                var classesId = $('#id').val();
                var requestURL = $('#requestURL').val();
                classesJoin(classesId,requestURL);
            })
        })
        .on('click', 'section.c-info-tab h4', function () { // 全部 最新 最热切换
            if ($(this).hasClass('active')) {
                return;
            }
            $(this).siblings().removeClass('active');
            var order = $(this).addClass('active').data('order');
            classify =null;
            classesTopicList(1,null,null);
        }).on("click",".question-tag",function(){
            var str = $(this).find('.classifyText').text();
            classify= checkClassify(str);
            classesTopicList(1,this,classify);
    }).on('click', 'div.class-slide-more__wrap button', function () { // 加载更多 加载话题
        if ($(this).hasClass('disabled')) {
            return;
        }
        var pageNo = $(this).data('page');
        var that = $(".question-ta.pitch_up");

        classesTopicList(pageNo,that,classify);
    });

    //按照班级话题分类加载话题列表
    function ajaxPersonalTopic(that,pageNo){
        var str = $(that).find('.classifyText').text();
        var classify = checkClassify(str);
        var params ={"classesId":$('#id').val(),"classify":classify} ;
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/web/classes/topic/ajaxTopicList',
            data:params,
            pageNo: pageNo,
            dataType:'text',
            type: 'post',
            async:false,
            pageCall: ajaxPersonalTopic,
            contentType:'application/json;charset=utf-8',
            callback : function(){
                $('[data-toggle=tooltip]').tooltip();
            },
            success:function (html) {
                $(html).insertBefore($('#topic-list span.loading').parent());
                var pageEnd = $('#pageEnd').val();
                $('span.loading').parent().addClass('hide');
                $('div.class-slide-more__wrap').toggleClass('hide', pageNo >= pageEnd);
                $('section.no-data__wrap').toggleClass('hide', pageEnd != 0);
                if (pageNo < pageEnd) {
                    jQuery('div.class-slide-more__wrap button')
                        .data('page', pageNo + 1)
                        .data('order', order)
                        .removeClass('disabled');
                }
                $('#pageEnd').remove();
                $('#topic-list > hr').remove();
                //图像懒加载
                lazyImage();
            },
            error:function (error) {
            }
        });

    }

    function checkClassify(list){
        var classify = null;
        if (list == "原创"){
            return classify = 1;
        }
        if (list == "讨论"){
            return classify = 2;
        }
        if (list == "疑问"){
            return classify = 3;
        }
        if (list == "活动"){
            return classify = 4;
        }
        if (list == "其他"){
            return classify = 5;
        }
    }

    /**
     * 加载班级成员
     */
    function classesMemberList() {
        $.ajax({
            url: baseloaction + '/web/classes/member/ajaxMemberList',
            data: {'classesId': classesId},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                if (response.success) {
                    appendClassesMember(response.entity.list);
                    lazyUserImage();
                }
            }
        })
    }

    function appendClassesMember(members) {
        if (members.length > 0) {
            $('#carousel-class-member a[data-slide]').show();
        } else {
            $('#carousel-class-member .carousel-inner span.no-data__wrap').show();
            return;
        }
        while (members.length > 0) {
            var count = members.length > 5 ? 5 : members.length;
            var array = members.splice(0, count);
            var html = $('<div class="item"><ol></ol></div>');
            array.forEach(function (v) {
                v.member.avatar = avatar(v);
                v.member.name = name(v);
                html.find('ol').append('<li>' +
                    '<p>' +
                    '<a href="javascript:void(0)">' +
                    '<img width="26" height="26" style="display: inline" class="img-circle vam lazy" data-original="' + v.member.avatar + '">' +
                    '<b class="c-666 fs12 ml5 vam">' + v.member.name + '</b> ' +
                    '</a>' +
                    '</p>' +
                    '</li>')
            });
            $('#carousel-class-member .carousel-inner').append(html);
        }
        $('#carousel-class-member .carousel-inner > div.item:first').addClass('active');
    }

    function avatar(param) {
        return param.member.avatar || staticPath + '/front/web/v3/img/default/default-face.gif';
    }

    function name(param) {
        var name = param.member.nickname || param.member.teacherName || '匿名';
        if (param.identity == 3) {
            name += '&nbsp;[班主任]';
        } else if (param.identity == 2) {
            name += '&nbsp;[管理员]';
        }
        return name;
    }

});